package com.fourWork.workFive;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class Server extends Thread {
    //声明ServerSocket对象
    ServerSocket serverSocket;
    //声明Socket对象
    Socket socket;
    //声明BufferedReader对象
    BufferedReader bufferedReader = null;
    //声明PrintStream对象
    PrintStream printStream = null;
    //声明List集合用于当前连接客户端的socket
    List<Socket> socketList = new ArrayList<>();

    public static void main(String[] args) {

        new Server().accept(8888);

    }

    /*
    接收客户端的连接请求
     */
    public void accept(int port) {
        try {
            serverSocket = new ServerSocket(port);
            System.out.println("等待客户端的连接请求...");
            while (true) {
                socket = serverSocket.accept(); //侦听并接收到此套接字的连接请求
                socketList.add(socket); //将当前客户端socket添加到list集合中
                System.out.println("客户端" + socket.getInetAddress() + "连接成功");
                //new ServerThread(socket,socketList).start();
                receive().start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != serverSocket) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /*
    接收客户端发送的信息
     */
    public Thread receive() {
        //创建线程
        return new Thread(() -> {
            System.out.println("this.socket:" + this.socket);

            //Server server = new Server();
//        Socket socket=this.socket;
//        System.out.println("socket:"+socket);
            try {
                //根据参数指定的引用来构造对象
                bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                //System.out.println("bufferedReader");
                while (true) {
                    //System.out.println("进入循环");
                    //读取客户端发送的内容
                    String s = bufferedReader.readLine();   //读取一行字符串并返回，返回null表示读取到末尾
                    System.out.println("接受客户端发送的内容为 ：" + s);
                    System.out.println(socketList);
                    //获取List集合中的socket对象
                    for (Socket socket1 : socketList) {
                        //根据参数指定的引用来构造对象
                        printStream = new PrintStream(socket1.getOutputStream());
                        printStream.println(s); //打印字符串后并终止该行
                        System.out.println("服务器发送数据成功");
                    }

                }


            } catch (IOException e) {
                System.out.println("--------进入删除---------");
                boolean remove = socketList.remove(socket);
                System.out.println(remove ? "删除成功" : "删除失败");
                e.printStackTrace();
            }
        });
    }


}





